<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-10-21 10:28
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Transformers;


use League\Fractal\TransformerAbstract;

class getModuleAndStoreStateTransformer extends TransformerAbstract
{
	private $roleModule;
	private $statisticsByStoreOrBarber;
	private $roleType;
	private $isWorkOpened;

	public function __construct($roleModule, $statisticsByStoreOrBarber, $roleType, $isWorkOpened)
	{
		$this->roleModule = $roleModule;
		$this->statisticsByStoreOrBarber = $statisticsByStoreOrBarber;
		$this->roleType = $roleType;
		$this->isWorkOpened = $isWorkOpened;
	}

	public function transform()
	{
		$result['modules'] = [];
		if (!empty($this->roleModule)) {
			foreach ($this->roleModule as &$v) {
				unset($v['pivot']);
			}
			unset($v);
			$result['modules'] = $this->roleModule;
		}

		if (config('constant.role.manager') == $this->roleType) {
			$result['is_open'] = is_array($this->isWorkOpened) ? in_array(0, array_column($this->isWorkOpened, 'is_satisfy')) ? 0 : 1 : $this->isWorkOpened;
			$result['conditions'] = $this->isWorkOpened;
		} else {
			$result['is_barber_open'] = $this->isWorkOpened;
		}
		if (!empty($this->statisticsByStoreOrBarber)) {
			$result['statistics'] = [     // 门店统计
			                                     'turnover'    => $this->statisticsByStoreOrBarber['turnOver'],
			                                     'appointment' => $this->statisticsByStoreOrBarber['orderAppointCount'],
			                                     'wait_time'   => $this->statisticsByStoreOrBarber['orderFinishedCount']
			];
		}
		return $result;
	}
}